<!--
 * @Author: your name
 * @Date: 2021-06-24 16:12:49
 * @LastEditTime: 2021-06-28 09:33:38
 * @LastEditors: Please set LastEditors
 * @Description: 资讯列表页面
 * @FilePath: \briup\项目\briup-ui\src\pages\admin\info\article\List.vue
-->
<template>
  <!-- 资讯列表页面 -->
  <div>
    <div class="header">
      <el-form
        :inline="true"
        :model="searchForm"
        class="demo-form-inline"
        size="small"
      >
        <el-form-item>
          <el-select v-model="searchForm.status" placeholder="状态" clearable>
            <el-option label="未审核" value="未审核" />
            <el-option label="审核通过" value="审核通过" />
            <el-option label="审核不通过" value="审核不通过" />
            <el-option label="推荐" value="推荐" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input v-model="searchForm.title" placeholder="标题" clearable />
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="searchForm.categoryId"
            placeholder="请选择栏目"
            clearable
          >
            <el-option
              v-for="item in categoryData"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
        <div style="float: right; text-align: right">
          <el-button
            type="primary"
            size="small"
            @click="toRelease"
          >发布风采</el-button>
        </div>
      </el-form>
    </div>
    <!-- 表格 -->
    <div class="table">
      <el-table
        v-loading="loading"
        :data="tableData"
        style="width: 100%"
        @row-click="toJump"
      >
        <el-table-column type="index" label="序号" width="50" />
        <el-table-column label="封面" align="center" width="200">
          <template slot-scope="scope">
            <!-- scope是当前temlate模板的作用域对象，row属性上存放的是当前行的数据对象 -->
            <el-image
              style="width: 150px; height: 100px"
              :src="scope.row.cover"
            />
          </template>
        </el-table-column>
        <el-table-column label="风采">
          <template slot-scope="scope">
            <div>{{ scope.row.title }}</div>
            <div>
              {{
                scope.row.baseUser === null ? "" : scope.row.baseUser.realname
              }}
            </div>
            <div>
              {{ scope.row.category === null ? "" : scope.row.category.name }}
            </div>
            <div>{{ scope.row.publishTime | dateFormat }}</div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="状态" width="200" prop="status">
          <template slot-scope="scope">
            <el-tag
              :type="
                scope.row.status === '审核通过'
                  ? 'success'
                  : scope.row.status === '审核不通过'
                    ? 'danger'
                    : scope.row.status === '推荐'
                      ? 'warning'
                      : 'info'
              "
            >{{ scope.row.status }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" align="center" width="200">
          <template slot-scope="scope">
            <el-button
              class="green_info"
              type="text"
              size="small"
              style="color: green"
              @click.stop="toRecommend(scope.row.id, scope.row.status)"
            >推荐</el-button>
            <el-button
              class="blue_info"
              type="text"
              size="small"
              @click.stop="toExamine(scope.row.id)"
            >审核</el-button>
            <el-button
              class="red_info"
              type="text"
              size="small"
              @click.stop="toDelete(scope.row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <div class="page-div">
      <el-pagination
        :current-page="params.page"
        :page-sizes="[10, 20, 30, 40, 50]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <el-dialog :visible.sync="visible" width="60%">
      <template slot="title">
        <div style="font-size: 18px; color: #fff">审核风采信息</div>
      </template>
      <el-form :data="form">
        <div style="text-align: center; font-size: 18px; line-height: 2em">
          {{ form.title }}
        </div>
        <div v-html="form.content" />
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          size="medium"
          type="danger"
          @click="NoPass"
        >审核不通过</el-button>
        <el-button
          size="medium"
          type="primary"
          @click="Pass"
        >审核通过</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { findAllcategory } from '@/api/infoCategory'
import {
  findAllpageQuery,
  articledeleteById,
  articlechangeStatus,
  articlefindById
} from '@/api/article'
export default {
  data() {
    return {
      searchForm: {
        title: '',
        status: '',
        authorId: '',
        categoryId: ''
      },
      categoryData: [],
      tableData: [],
      // 分页参数
      params: {
        page: 1,
        pageSize: 10
      },
      // 总条数
      total: 0,
      loading: false,
      form: [],
      visible: false
    }
  },
  computed: {},
  watch: {
    // 深度监听分页参数
    params: {
      handler() {
        this.findAllpageQuerytableData()
      },
      deep: true
    }
  },
  created() {
    this.findcategoryData()
    this.findAllpageQuerytableData()
  },
  mounted() {},
  methods: {
    // 查询栏目分类
    async findcategoryData() {
      const res = await findAllcategory()
      this.categoryData = res.data
    },
    // 分页查询风采信息
    async findAllpageQuerytableData() {
      this.loading = true
      const temp = {
        ...this.params,
        ...this.searchForm
      }
      // 遍历对象
      for (const key in temp) {
        // key是一个变量，代表的属性名
        // key是属性名,temp[key]是属性值
        // 删除对象属性
        if (!temp[key]) {
          delete temp[key]
        }
      }
      const res = await findAllpageQuery(temp)
      this.tableData = res.data.list
      this.total = res.data.total
      this.loading = false
    },
    // 删除按钮点击事件处理程序
    toDelete(id) {
      this.$confirm('此操作将永久删除该信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const res = await articledeleteById({ id })
          this.$message({ type: 'success', message: res.message })
          this.findAllpageQuerytableData()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 推荐按钮点击事件处理程序
    async toRecommend(id) {
      this.$confirm('您确定要推荐此资讯吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const res = await articlechangeStatus({ id, status: '推荐' })
          this.$message({ type: 'success', message: res.message })
          this.findAllpageQuerytableData()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消推荐'
          })
        })
    },
    // 审核按钮点击事件处理程序
    async toExamine(id) {
      const res = await articlefindById({ id })
      this.form = res.data
      this.visible = true
    },
    // 审核通过按钮点击事件处理程序
    async NoPass() {
      const id = this.form.id
      this.visible = false
      const res = await articlechangeStatus({ id, status: '审核不通过' })
      this.$message({ type: 'success', message: res.message })
      this.findAllpageQuerytableData()
    },
    // 审核不通过按钮点击事件处理程序
    async Pass() {
      const id = this.form.id
      this.visible = false
      const res = await articlechangeStatus({ id, status: '审核通过' })
      this.$message({ type: 'success', message: res.message })
      this.findAllpageQuerytableData()
    },
    // 每页显示的条数发生变化时的事件处理程序
    handleSizeChange(val) {
      this.params = {
        page: 1,
        pageSize: val
      }
    },
    // 当前页码发生变化时的事件处理程序
    handleCurrentChange(val) {
      this.params.page = val
    },
    onSubmit() {
      this.findAllpageQuerytableData()
    },
    toJump(row) {
      this.$router.push({
        path: '/details',
        query: { id: row.id }
      })
    },
    toRelease() {
      this.$router.push({
        path: '/publish'
      })
    }
  }
}
</script>
<style>
.el-dialog__header {
  border-bottom: 1px solid #2cb5ac;
  background-color: #2cb5ac;
}
.el-dialog__footer {
  border-top: 2px solid #2cb5ac;
}
.el-dialog__close el-icon el-icon-close {
  color: #fff;
}
</style>
